package Dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSON;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;



import wendy.watson.mapping.*;

@Repository(value = "userDao")
public class UserDaoImp implements UserDao{
	
	@Resource
	private SqlSessionTemplate sqlSessionTemplate;

	public SqlSessionTemplate getSqlSessionTemplate() {
		return sqlSessionTemplate;
	}

	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		this.sqlSessionTemplate = sqlSessionTemplate;
	}

	public UserDaoImp() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public int add(HashMap<String, Object> map) {
		// TODO Auto-generated method stub
		String statement = "wendy.watson.mapping.userMapper.insertUser";
		int flag = sqlSessionTemplate.insert(statement, map);
		//sqlSessionTemplate.commit();
		
		String statement0 = "wendy.watson.mapping.userMapper.topping";
		User user = (User)sqlSessionTemplate.selectOne(statement0,null);
		System.out.println(user);
		System.out.println("flag="+ flag);
		
		
		System.out.println(" 数据库操作成功？："+flag);
		return flag;
	}

	@Override
	public String show(HashMap<String, Object> map) {
		// TODO Auto-generated method stub
		String statement1 = "wendy.watson.mapping.userMapper.filterlist";
		String statement2 = "wendy.watson.mapping.userMapper.count_filter";
	  
	
		int RowCount = sqlSessionTemplate.selectOne(statement2, map);
        System.out.println("RowCount "+RowCount);
		List<User> listUsers0 = sqlSessionTemplate.selectList(statement1, map);
		ArrayList<Object> list0 = new ArrayList<Object>();
		for (User a : listUsers0) {
			HashMap<String, String> map0 = new HashMap<String, String>();
			map0.put("id", a.getId() + "");
			map0.put("name", a.getName() + "");
			map0.put("content", a.getContent() + "");
			map0.put("top", a.getTop() + "");
			
			String jsonString0 = JSON.toJSONString(map0);
			list0.add(jsonString0);
		}
		
		String list = "{\"total\" : " + RowCount + ", \"rows\" : " + list0
				+ "}";
		System.out.println("获得的json"+list);
		return list;
	}

	@Override
	public int destroy(HashMap<String, Object> map) {
		// TODO Auto-generated method stub
		String statement = "wendy.watson.mapping.userMapper.deleteUser";
		int flag = sqlSessionTemplate.update(statement, map);
		
		return flag;
	}

	@Override
	public String gain(HashMap<String, Object> map) {
		// TODO Auto-generated method stub
		String statement = "wendy.watson.mapping.userMapper.gainUser";
		User user = (User)sqlSessionTemplate.selectOne(statement,map);
		String jsonlist = JSON.toJSONString(user);
		System.out.println(jsonlist);
		return jsonlist;
	}

	@Override
	public int edit(HashMap<String, Object> map) {
		// TODO Auto-generated method stub
		String statement = "wendy.watson.mapping.userMapper.updateUser";
		int flag = sqlSessionTemplate.update(statement, map);
		//sqlSessionTemplate.commit();
		
		String statement0 = "wendy.watson.mapping.userMapper.topping";
		User user = (User)sqlSessionTemplate.selectOne(statement0,null);
		System.out.println(user);
		System.out.println("flag="+ flag);
		
		
		System.out.println(" 修改日志操作成功？："+flag);
		return flag;
	}

}
